/*=====================================================================================================================
                    Copyright(c) 2009 Geometric PLM Software Corp. All rights reserved.
                             Unpublished - All rights reserved
=======================================================================================================================
File description:
    Filename: tc_ico.c
    Module  : (geometric) itk module.

        This file manages the operations of the ico.

=======================================================================================================================
Date               Name              Description of Change
14-Jul-2009        Penn,Xie          Initialize creation
$HISTORY$
=====================================================================================================================*/
// {{ FOR IMAN 
#ifdef BUILD_IMAN 
#include <iman_util.h>
#include <imantype.h>

#define GTC_strcmp          iman_strcmp
#define GTC_strcasecmp      iman_strcasecmp
#define GTC_free_strings    IMAN_free_strings

#else // Teamcenter
#include <tc\tc_util.h>
#include <tccore\tctype.h>

#define GTC_strcmp          tc_strcmp
#define GTC_strcasecmp      tc_strcasecmp
#define GTC_free_strings    TC_free_strings
#endif
// }}

#include <tccore\aom.h>
#include <ics/ics.h>
#include <ics/ics2.h>

#include "string_utils.h"
#include "tc_ico.h"

logical GICO_is_attr_exist( tag_t icoTag, const char *attrName )
{
    logical bFind        = false;

    int     i            = 0;
    int     ifail        = ITK_ok;
    int     attrCnt      = 0;
    char    **attrNames  = NULL;
    char    **attrValues = NULL;

    ifail = ICS_ask_attributes_of_classification_obj(icoTag, &attrCnt, &attrNames, &attrValues);
    if (ifail != ITK_ok)
        return false;

    for (i = 0; i < attrCnt; i++)
    {
        if (GTC_strcmp( attrNames[i], attrName ) == 0)
        {
            bFind = true;
            break;
        }
    }

    MEM_free( attrNames );
    MEM_free( attrValues );

    return bFind;
}

int GICO_get_attr_value( tag_t icoTag, const char *attrName, char **attrValue )
{
    int    ifail        = ITK_ok;

    int    i            = 0;
    int    len          = 0;
    int    attrCnt      = 0;
    char   **attrNames  = NULL;
    char   **attrValues = NULL;

    *attrValue = NULL;

    ifail = ICS_ask_attributes_of_classification_obj( icoTag, &attrCnt, &attrNames, &attrValues );
    if (ifail != ITK_ok)
        return ifail;

    for (i = 0; i < attrCnt; i++)
    {
        if ( GTC_strcasecmp( attrNames[i], attrName ) == 0 )
        {
            GSTR_clone( attrValue, attrValues[i] );
            break;
        }
    }

    // ICS_ask_attribute_value (tag_t classificationObject, const char *attributeName, char **attributeValue) 
    //
    GTC_free_strings( attrNames );
    GTC_free_strings( attrValues );

    return ifail;
}

int GICO_get_attr_lov_values(const char* classID, const char *attrName, logical *isLov, int *lovNumber, char ***lovKeys, char ***lovValues)
{
    int attrCount = 0, i = 0, ifail = ITK_ok;
    tag_t *tags = NULL;
    int *ids = NULL;
    tag_t parentTag = NULL_TAG;
    char *parentID = NULL;

    *isLov = false;
    *lovNumber = 0;
    *lovValues = NULL;
    
    ifail = ICS_class_ask_parent(classID,  &parentTag, &parentID);
    if(ifail != ITK_ok)
    {
        return ifail;
    }
    if(parentTag != NULL_TAG)
    {
        ifail = GICO_get_attr_lov_values(parentID, attrName, isLov, lovNumber, lovKeys, lovValues);
        MEM_free(parentID);
        if(ifail != ITK_ok)
        {
            return false;
        }
        if(*isLov == true)
        {
            return ITK_ok;
        }
    }

    ifail = ICS_class_ask_attributes(classID, &attrCount, &tags, &ids);
    if(ifail != ITK_ok)
    {
        return ifail;
    }
    for(i=0; i<attrCount; i++)
    {
        char* propertyName = NULL;
        ifail = ICS_attribute_ask_property(ids[i],"NAME", &propertyName);
        if(ifail != ITK_ok)
        {
            MEM_free(ids);
            return ifail;
        }
        if(GTC_strcmp(propertyName, attrName) == 0)
        {
            char *formatID = NULL;
            ifail = ICS_attribute_ask_property(ids[i],"FORMAT1", &formatID);
            if(ifail != ITK_ok)
            {
                MEM_free(ids);
                MEM_free(propertyName);
                return ifail;
            }
            if(formatID != NULL && GTC_strcmp(formatID, "") != 0 && formatID[0] == '-')
            {
                char *lovName = NULL, *owningSite = NULL;
                int options = 0, sharedSiteCount = 0;
                char **sharedSites = NULL;
                *isLov = true;
                ifail = ICS_keylov_describe(formatID, &lovName, &options, lovNumber, lovKeys, lovValues, &owningSite, &sharedSiteCount, &sharedSites);
                if(ifail == ITK_ok)
                {
                    MEM_free(lovName);
                    MEM_free(owningSite);
                    GTC_free_strings(sharedSites);
                }
                MEM_free(ids);
                MEM_free(propertyName);
                MEM_free(formatID);
                return ifail;
            }
            MEM_free(formatID);
        }
        MEM_free(propertyName);
    }
    MEM_free(ids);
    return ifail;
    

}
